﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExpressionTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExpressionUtil.Expression exp = new ExpressionUtil.Expression(this.textBox1.Text);
            try
            {
//#if DEBUG
                System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
//#endif
                exp.Compile();
//#if DEBUG
                watch.Stop();
                System.Windows.Forms.MessageBox.Show("Compile spend:" + watch.ElapsedMilliseconds.ToString() + "ms");
                //System.Diagnostics.Debug.WriteLine("Compile spend:" + watch.ElapsedMilliseconds.ToString() + "ms");
                watch.Reset();
                watch.Start();
//#endif
                string[] variables = exp.VariableSymbols;
                for (int i = 0; i < variables.Length; i++)
                {
                    exp.SetVariableValue(variables[i], i + 1);
                }
                ExpressionUtil.ExecuteRunTimeType type = exp.Execute();
//#if DEBUG
                watch.Stop();
                System.Windows.Forms.MessageBox.Show("Execute spend:" + watch.ElapsedMilliseconds.ToString() + "ms");
                //System.Diagnostics.Debug.WriteLine("Execute spend:" + watch.ElapsedMilliseconds.ToString() + "ms");
//#endif
                System.IO.FileStream s = null;
                try
                {
                    s = new System.IO.FileStream("test.bin", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    ExpressionUtil.Expression.Serialize(s, exp);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("序列化错误：" + ex.ToString());
                }
                finally
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                }
                MessageBox.Show("计算结果：" +
                    (type == ExpressionUtil.ExecuteRunTimeType.Decimal ? exp.DecimalResult.ToString() : exp.BoolResult.ToString()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "*.bin|*.bin";
                dialog.CheckFileExists = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    System.IO.FileStream s = null;
                    try
                    {
                        s = new System.IO.FileStream("test.bin", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        ExpressionUtil.Expression exp = ExpressionUtil.Expression.Deserialize(s);
                        if (exp == null)
                        {
                            throw new Exception("获取的序列化表达式为null。");
                        }
                        this.textBox1.Text = exp.Expr;
                        string[] variables = exp.VariableSymbols;
                        for (int i = 0; i < variables.Length; i++)
                        {
                            exp.SetVariableValue(variables[i], i + 1);
                        }
                        ExpressionUtil.ExecuteRunTimeType type = exp.Execute();
                        MessageBox.Show("计算结果：" +
                            (type == ExpressionUtil.ExecuteRunTimeType.Decimal ? exp.DecimalResult.ToString() : exp.BoolResult.ToString()));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("反序列化错误：" + ex.ToString());
                    }
                    finally
                    {
                        if (s != null)
                        {
                            s.Close();
                        }
                    }
                }
            }
        }
    }
}
